define(['/js/config.js'], () => {
	require(['template','urlSet','header','footer','fly','aside'],(template,urlSet,header) => {
		class Shop {
			constructor() {
				console.dir($)
			    this.renderShop().then(() => {
					this.Shopping()
					this.addCar()
					this.cartCount($('#div_box-right-8-div-1'))
					this.asideShow()
				})
			}
			
			renderShop () {
				return new Promise(resolve => {
					const id = location.search.slice(4)
					$.get(urlSet.detailUrl+id,resp => {
						if (resp.code === 200){
							var descContentList1 = resp.data.detail.descContentList.filter(descContentList => descContentList.photo)
							let descContentList = descContentList1.slice(3,11)
							
							const {
								id,
								accessoryHints,
								title,
								introduction,
								tbOriginPrice,
								photo,
								image,
								saleNum,
								favoritedCount,
								shop,
								originPrice
								} = resp.data.detail
							this.detail = {
								id,
								accessoryHints,
								title,
								introduction,
								tbOriginPrice,
								photo,
								image,
								saleNum,
								favoritedCount,
								shop,
								originPrice,
								descContentList
							}
							$('main').html(template('detailtemplate',{...this.detail}))
							resolve()
						}
					})
				})
			}
			
			Shopping () {
				//数量选择
				let n = 1
				$('#div_box-right-7-div-2-onck1').on('click',function () {
					$('#div_box-right-7-div-1').html(++n)
				})
				
				$('.att').on('mousedown', function () {
					$(this).addClass('bc1')
				})
				$('.att').on('mouseup', function () {
					$(this).removeClass('bc1')
				})
				
				$('#div_box-right-7-div-2-onck2').on('click',() => {
					$('#div_box-right-7-div-1').html(--n < 1 ? n = 1 : n)
				})
				
				// 划过更改底部图片
				$('#div_box-bottom-left').on('mouseover','.div_box-bottom',function () {
					$('.smallImg img').prop('src',$(this).find('img').prop('src'))
					$('.bigImg img').prop('src',$(this).find('img').prop('src'))
					$(this).addClass('ac1').siblings().removeClass('ac1')
				})
				//点击更改右部图片
				$('#div_box-right-6').on('click','.colorlist',function () {
					$('.smallImg img').prop('src',$(this).find('img').prop('src'))
					$('.bigImg img').prop('src',$(this).find('img').prop('src'))
					$(this).addClass('ac2').siblings().removeClass('ac2')
				})
				//放大镜效果
				$('.smallImg').on('mouseover',() => {
					$('#shadow').show()
					$('.bigImg').show()
				})
				$('.smallImg').on('mouseout',() => {
					$('#shadow').hide()
					$('.bigImg').hide()
				})
				$(document).on('mousemove',function (e) {
					var left = e.pageX - $('#shadow').width()/2 - $('.smallImg').offset().left,
						top = e.pageY - $('#shadow').height()/2 - $('.smallImg').offset().top
						
						if (left < 0) {
							left = 0
						}else if (left > $('.smallImg').width()-$('#shadow').width()){
							left = $('.smallImg').width()-$('#shadow').width()
						}
						if (top < 0) {
							top = 0
						}else if (top > $('.smallImg').height()-$('#shadow').height()){
							top = $('.smallImg').height()-$('#shadow').height()
						}
						$('#shadow').css({left,top})
						
						//datu 
						
						left = -1.35 * left 
						top = -1.35 * top
						$('.bigImg img').css({left,top})
				})
				
			}
			
			addCar () {
				$('#div_box-right-8-div-2').on('click',(e) => {
					this.fly(e)
					let cart = localStorage.getItem('cart')
					var num = Number($('#div_box-right-7-div-1').html())
					if (cart) {
						cart = JSON.parse(cart)
						
						if(cart.some(shop => shop.id === this.detail.id)){
							cart = cart.map(shop => {
								if (shop.id === this.detail.id) {
									shop.count += num
								}
								return shop
							})
						}else{
							cart.push({
								...this.detail,
								count:num,
								check:true,
								})
						}
						localStorage.setItem('cart',JSON.stringify(cart))
					}else {
						localStorage.setItem('cart',JSON.stringify([{
							...this.detail,
							count :num,
							check:true
							}]))
					}
				})
			}
			
			cartCount (ele) {
				ele.on('click',(e) => {
					let cart = localStorage.getItem('cart')
					var num = Number($('#div_box-right-7-div-1').html())
					if (cart) {
						cart = JSON.parse(cart)
						
						if(cart.some(shop => shop.id === this.detail.id)){
							cart = cart.map(shop => {
								if (shop.id === this.detail.id) {
									shop.count += num
								}
								return shop
							})
						}else{
							cart.push({...this.detail,count:num})
						}
						localStorage.setItem('cart',JSON.stringify(cart))
					}else {
						localStorage.setItem('cart',JSON.stringify([{
							...this.detail,
							count :num
							}]))
					}
					window.open(`/html/shopcar.html`,'_self')
				})
			}
			
			fly (e) {
				$(`<img style="width: 30px; height: 30px; border-radius: 50%" src="${this.detail.photo[0].url}">`).fly({
					start:{
						left: e.clientX,  //开始位置（必填）#fly元素会被设置成position: fixed
						top: e.clientY,  //开始位置（必填）
					},
					end:{
						left: $('#shopCart').offset().left - $(window).scrollLeft(), //结束位置（必填）
						top: $('#shopCart').offset().top - $(window).scrollTop()  //结束位置（必填）
					},
					speed: 0.7, //越大越快，默认1.2
					onEnd: function () {
						this.destroy()
						// 调用header的方法重新计算购物车数量
						header.calcCart()
					} //结束回调
				})
			}
			
				asideShow () {
					$(".quick_links_panel li").mouseenter(function(){
					
							$(this).children(".mp_tooltip").animate({left:-92,queue:true});
					
							$(this).children(".mp_tooltip").css("visibility","visible");
					
							$(this).children(".ibar_login_box").css("display","block");
					
					});
					$(".quick_links_panel li").mouseleave(function(){
				
						$(this).children(".mp_tooltip").css("visibility","hidden");
				
						$(this).children(".mp_tooltip").animate({left:-121,queue:true});
				
						$(this).children(".ibar_login_box").css("display","none");
				
					});
					$(".quick_toggle li").mouseover(function(){
				
						$(this).children(".mp_qrcode").show();
				
					});
				
					$(".quick_toggle li").mouseleave(function(){
				
						$(this).children(".mp_qrcode").hide();
				
					});
				}
			
		}
		return new Shop()
	})
})